﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using OLMS.DAL;

namespace OLMS.BLL
{
    public class UserBLL
    {
        //用户登录
        public static Users Login(string loginId, string loginPwd)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                //密码加密
                loginPwd = PasswordEncryption(loginPwd);
                //根据将密码的密文与用户表中密码的密文比较来确定用户密码是否正确
                Users user = db.Users.FirstOrDefault(u => u.LoginId == loginId && u.LoginPwd == loginPwd);
                return user;
            }
        }
        //用户注册       
        public static bool Register(Users user)
        {
            //密码加密
            user.LoginPwd = PasswordEncryption(user.LoginPwd);
            using (OLMSDBEntities db = new OLMSDBEntities())
            {             
                db.Users.Add(user);
                return db.SaveChanges() > -1 ? true : false;
            }
        }

        public static Users GetUsersById(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {               
                Users user = db.Users.SingleOrDefault(u => u.UserId == userId);
                return user;
            }
        }


        public static bool ChangeRole(int userId,int roleId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Users user = db.Users.SingleOrDefault(u => u.UserId == userId);
                user.RoleId = roleId;
                return db.SaveChanges() != -1 ? true : false;
            }
        }
        public static bool RemoveUser(int userId)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                Users user = db.Users.SingleOrDefault(u => u.UserId == userId);
                user.UserStatus = user.UserStatus == 0 ? 1 : 0;
                return db.SaveChanges() != -1 ? true : false;
            }
        }

        //查询全部启用的普通用户
        public static List<Users> GetUsersList()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Users.Where(u => u.RoleId != 1 && u.UserStatus == 1).ToList();
            }
        }

        //查询全部启用的管理员用户
        public static List<Users> GetAdminUsersList()
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {
                return db.Users.ToList();
            }
        }

        //更改密码
        public static bool ChangePassword(int userId,string oldPwd,string newPwd)
        {
            using (OLMSDBEntities db = new OLMSDBEntities())
            {

                Users users = db.Users.SingleOrDefault(u => u.UserId == userId && u.LoginPwd == PasswordEncryption(oldPwd));
                if (users != null)
                {
                    users.LoginPwd = PasswordEncryption(newPwd);
                    return db.SaveChanges() > -1 ? true : false;
                }
                else
                {
                    return false;
                }
            }
        }

        //实用SHA1加密密码
        private static string PasswordEncryption(string pwd)
        {
            //创建SHA1加密算法对象
            SHA1 sha1 = SHA1.Create();
            //将原始密码转换为字节数组
            byte[] originalPwd = Encoding.UTF8.GetBytes(pwd);
            //执行加密
            byte[] encryPwd = sha1.ComputeHash(originalPwd);
            //将加密后的字节数组转换为大写字符串
            return string.Join("", encryPwd.Select(b => string.Format("{0:x2}", b)).ToArray()).ToUpper();
        }

    }
}